package com.zyz.C_数组.练习;

/**
 * @author: vick
 * @Date: 2025/3/13
 * @Description:
 * @version: 1.0
 */
public class Demo4 {
    public static void main(String[] args) {
        //原数组
        int[] nums = {1, 2, 2, 3, 1, 4, 3, 1, 5};

        //存储元素中不同的值
        int[] uniqueNums = new int[nums.length]; // 1 2 3 4 5
        //记录uniqueNums数组中对应元素出现的次数
        int[] counts = new int[nums.length];    // 3 2 2 1  1
        //记录uniqueNums数组中实际存储的不同元素的数量
        int uniqueCount = 0; // 4 个

        //遍历数组
        for (int num : nums) {
            boolean flag = false;
            for (int i = 0; i < uniqueCount; i++) {
                if (num == uniqueNums[i]) {
                    counts[i]++;
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                uniqueNums[uniqueCount] = num;
                counts[uniqueCount] = 1;
                uniqueCount++;
            }
        }

        for (int i = 0; i < uniqueCount ; i++) {
            System.out.println("元素：" + uniqueNums[i] + "出现了" + counts[i] + "次");
        }
    }
}
